﻿Imports System.Data.OleDb
Imports System.Net.Mail
Imports System.IO
Imports System.Text

Partial Public Class products
    Inherits System.Web.UI.Page
    Public Shared conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("VerisEdiConnectionString").ToString())
    Dim lbl_HardwareCategory As Label = New Label()
    Dim lbl_HardwareSubCategory As Label = New Label
    Dim subCategory As String = ""
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Session.Clear()
            MultiView1.SetActiveView(view_Products)
        End If
        If IsPostBack Then
            If cbl_Tests.SelectedIndex = -1 Then
                panel_Hardware.Visible = False
            Else
                panel_Hardware.Visible = True
            End If
        End If
    End Sub
    Private Sub selectSoftwareLevel(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbl_Tests.SelectedIndexChanged
        Dim softwareLevel As Integer = getRequiredSoftwareLevel()
        Session("sLevel") = softwareLevel
        repeater_Software.DataBind()
        Dim requiredCategory As Integer = getRequiredCategory()
        Session("sStiCat") = requiredCategory
        rp_Sti.DataBind()
    End Sub
    Private Function creatListOfSelectedTests() As List(Of Integer)
        Dim listOfSelectedTests As List(Of Integer) = New List(Of Integer)
        For Each item In cbl_Tests.Items
            If item.Selected Then
                Dim testId As Integer = item.Value
                listOfSelectedTests.Add(testId)
            End If
        Next
        Return listOfSelectedTests
    End Function

    Private Function getRequiredSoftwareLevel() As Integer
        Dim requiredSoftwareLevel As Integer = 0
        Dim rqdSftLvlList As List(Of Integer) = New List(Of Integer)()
        If conn.State = ConnectionState.Open Then
            conn.Close()
        End If
        conn.Open()
        For Each item As ListItem In cbl_Tests.Items
            Dim sql As String = "SELECT RequiredSoftware FROM Tests WHERE ID = @ID"
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            If item.Selected Then
                cmd.Parameters.AddWithValue("@ID", item.Value)
                Dim reader As OleDbDataReader = cmd.ExecuteReader()
                If reader.Read() Then
                    requiredSoftwareLevel = CType(reader("RequiredSoftware"), Integer)
                    rqdSftLvlList.Add(requiredSoftwareLevel)
                    reader.Close()
                End If
            End If
        Next
        conn.Close()
        If rqdSftLvlList.Contains(2) Then
            requiredSoftwareLevel = 2
            Session("sOLevel") = requiredSoftwareLevel + 1
            Return requiredSoftwareLevel
        ElseIf rqdSftLvlList.Contains(1) Then
            requiredSoftwareLevel = 1
            Session("sOLevel") = 0
        Else
            requiredSoftwareLevel = 0
            Session("sOLevel") = 0
        End If

        Return requiredSoftwareLevel
    End Function

    Private Sub createHyperLinkForCBL(ByVal cbl As CheckBoxList)
        Dim path As String = Me.ResolveUrl("~/details.aspx?name=")
        For i As Integer = 0 To cbl.Items.Count - 1
            Dim productName As String = cbl.Items(i).Text
            'productName = productName.Replace(" ", String.Empty)
            cbl.Items(i).Text = " <a href='" + path + productName + "' title='View Details'>" + cbl.Items(i).Text + "</a>"
        Next
    End Sub
    Private Function getRequiredCategory() As Integer
        Dim requiredCategory As Integer = 0
        Dim selectedTests As List(Of Integer) = creatListOfSelectedTests()
        If selectedTests.Contains(3) Or selectedTests.Contains(4) Or selectedTests.Contains(5) Or selectedTests.Contains(6) Then
            requiredCategory = 2

        ElseIf selectedTests.Contains(1) Or selectedTests.Contains(2) Then
            requiredCategory = 1
        End If
        Return requiredCategory
    End Function

    Protected Sub rp_Comp_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim cb_Computer As CheckBox = TryCast(e.Item.FindControl("cb_Computer"), CheckBox)
            Dim lbl_Computer As Label = TryCast(e.Item.FindControl("lbl_Computer"), Label)
            Dim cbl_Computer As CheckBoxList = TryCast(e.Item.FindControl("cbl_Computer"), CheckBoxList)
            Dim sql As String = "SELECT * FROM ComputerQuery WHERE BrandName = @BrandName"
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            If conn.State = ConnectionState.Open Then
                conn.Close()
            End If
            conn.Open()
            cmd.Parameters.AddWithValue("@BrandName", lbl_Computer.Text)
            Dim reader As OleDbDataReader = cmd.ExecuteReader()
            cbl_Computer.DataSource = reader
            cbl_Computer.DataValueField = "ID"
            cbl_Computer.DataTextField = "ModelNo"
            cbl_Computer.DataBind()
            reader.Close()
            conn.Close()
            createHyperLinkForCBL(cbl_Computer)
        End If
    End Sub

    Protected Sub rp_Amp_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim lbl_Amp As Label = TryCast(e.Item.FindControl("lbl_Amp"), Label)
            Dim cbl_AmpCh As CheckBoxList = TryCast(e.Item.FindControl("cbl_AmpCh"), CheckBoxList)
            Dim sql As String = "SELECT * FROM AmplifierQuery WHERE AmplifierName = @AmplifierName"
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            If conn.State = ConnectionState.Open Then
                conn.Close()
            End If
            conn.Open()
            cmd.Parameters.AddWithValue("@AmplifierName", lbl_Amp.Text)
            Dim reader As OleDbDataReader = cmd.ExecuteReader()
            cbl_AmpCh.DataSource = reader
            cbl_AmpCh.DataValueField = "ID"
            cbl_AmpCh.DataTextField = "ChannelName"
            cbl_AmpCh.DataBind()
            reader.Close()
            conn.Close()
            createHyperLinkForCBL(cbl_AmpCh)
        End If
    End Sub

    Protected Sub rp_Sti_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim lbl_Sti As Label = TryCast(e.Item.FindControl("lbl_Sti"), Label)
            Dim cbl_Sti As CheckBoxList = TryCast(e.Item.FindControl("cbl_Sti"), CheckBoxList)
            Dim sql As String = "SELECT * FROM StimulatorQuery WHERE TypeName = @TypeName"
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            If conn.State = ConnectionState.Open Then
                conn.Close()
            End If
            conn.Open()
            cmd.Parameters.AddWithValue("@TypeName", lbl_Sti.Text)
            Dim reader As OleDbDataReader = cmd.ExecuteReader()
            cbl_Sti.DataSource = reader
            cbl_Sti.DataValueField = "ID"
            cbl_Sti.DataTextField = "StimulatorName"
            cbl_Sti.DataBind()
            reader.Close()
            conn.Close()
            createHyperLinkForCBL(cbl_Sti)
        End If
    End Sub

    Protected Sub cbl_SystemPlatform_DataBound(sender As Object, e As EventArgs)
        Dim cbl As CheckBoxList = TryCast(sender, CheckBoxList)
        createHyperLinkForCBL(cbl)
        For Each item As ListItem In cbl.Items
            item.Selected = True
        Next
    End Sub

    Protected Sub btn_Quote_Click(sender As Object, e As EventArgs)
        Dim products As ListDictionary = New ListDictionary()
        Dim requestList As StringBuilder = New StringBuilder()
        For Each test As ListItem In cbl_Tests.Items
            If test.Selected Then
                products.Add(products.Count & " Tests: ", test.Text)
            End If
        Next
        If repeater_Software.HasControls Then
            For Each software As RepeaterItem In repeater_Software.Items
                Dim cb_Selected As CheckBox = TryCast(software.FindControl("cb_Selected"), CheckBox)
                If cb_Selected.Checked Then
                    Dim selectedSoftwareLevel As Label = TryCast(software.FindControl("SoftwareLevelLabel"), Label)
                    Dim selectedSoftwareName As Label = TryCast(software.FindControl("ProductNameLabel"), Label)
                    products.Add(products.Count & " Software Level " & selectedSoftwareLevel.Text & ": ", selectedSoftwareName.Text)
                End If
            Next
        End If
        For Each systemItem As ListItem In cbl_SystemPlatform.Items
            If systemItem.Selected Then
                products.Add(products.Count & " System Platform " & systemItem.Value.ToString() & ": ", systemItem.Text)
            End If
        Next
        If rp_Comp.HasControls Then
            For Each comp As RepeaterItem In rp_Comp.Items
                Dim category As Label = comp.FindControl("lbl_Computer")
                Dim cbl As CheckBoxList = comp.FindControl("cbl_Computer")
                For Each selectedModel As ListItem In cbl.Items
                    If selectedModel.Selected Then
                        products.Add(products.Count & " " & category.Text, selectedModel.Text)
                    End If
                Next
            Next
        End If
        If rp_Amp.HasControls Then
            For Each amp As RepeaterItem In rp_Amp.Items
                Dim ampName As Label = TryCast(amp.FindControl("lbl_Amp"), Label)
                Dim cbl_Ch As CheckBoxList = TryCast(amp.FindControl("cbl_AmpCh"), CheckBoxList)
                For Each ch As ListItem In cbl_Ch.Items
                    If ch.Selected Then
                        products.Add(products.Count & " " & ampName.Text, ch.Text)
                    End If
                Next
            Next
        End If
        If rp_Sti.HasControls Then
            For Each stiItem As RepeaterItem In rp_Sti.Items
                Dim stiType As Label = TryCast(stiItem.FindControl("lbl_Sti"), Label)
                Dim cbl_Sti As CheckBoxList = TryCast(stiItem.FindControl("cbl_Sti"), CheckBoxList)
                For Each sti As ListItem In cbl_Sti.Items
                    If sti.Selected Then
                        products.Add(products.Count & " " & stiType.Text, sti.Text)
                    End If
                Next
            Next
        End If
        Dim appPath As String = Request.PhysicalApplicationPath
        Dim writer As StreamWriter = New StreamWriter(appPath & "EmailTemplates\QuoteRequest.txt")
        writer.WriteLine("Hi, <br> I am interested in the following products. <br> Please send a quote for the same. <br>")
        Dim mailMessage As MailMessage = New MailMessage()
        Dim mailCopy As MailMessage = New MailMessage()
        mailMessage.IsBodyHtml = True
        mailCopy.IsBodyHtml = True
        mailMessage.From = New MailAddress("jayakishnani@hotmail.com", tb_Name.Text)
        mailCopy.From = New MailAddress("jayakishnani@hotmail.com", "Veris Edi")
        mailMessage.To.Add(New MailAddress("edifb200@gmail.com"))
        mailCopy.To.Add(New MailAddress(tb_Email.Text))
        mailMessage.Subject = "Request for a quote"
        mailCopy.Subject = "We have received your request for a qoute"
        requestList.Append("Hi " + tb_Name.Text + ", <br>").AppendLine()
        requestList.Append("Thank you for requesting a qoute from EDI. We have recived your request and are working on it <br>").AppendLine()
        requestList.AppendLine()
        requestList.Append("You have requested a quote for the following: <br>").AppendLine()
        For Each item As DictionaryEntry In products
            Dim key As String = item.Key.ToString()
            Dim value As String = item.Value.ToString()
            requestList.Append(value + " <br>").AppendLine()
            writer.WriteLine("<br>" & value.ToString())
            writer.WriteLine("<br>")
        Next
        requestList.Append("One of our team members will contact you shortly. <br>").AppendLine()
        requestList.Append("Thank You. <br> ").AppendLine()
        requestList.Append("Team EDI").AppendLine()
        writer.WriteLine("<br> Additional Information: <br>" + tb_Message.Text + "")
        writer.WriteLine("<br> Following are my details:<br>" + tb_Title.Text + "<br>" _
                         + "<br>" + tb_Name.Text + "<br>" + tb_Company.Text + "<br>" + tb_Add1.Text + "<br>" _
                         + tb_Add2.Text + "<br>" + tb_Add3.Text + "<br>" + tb_City.Text + "<br>" + tb_State.Text + "<br>" _
                         + tb_Zip.Text + "<br>" + tb_Country.Text + "<br> <br>" + tb_Phone.Text + "<br>" _
                         + tb_Fax.Text + "<br>" + tb_Email.Text)
        If Not String.IsNullOrEmpty(tb_Message.Text) Then
            writer.WriteLine("<br> Custom Request: <br>" + tb_Message.Text)
        End If
        writer.WriteLine("<br> <br> Regards,")
        writer.WriteLine("<br>")
        writer.WriteLine("<br>" + tb_Name.Text)
        writer.Close()
        Dim reader As StreamReader = New StreamReader(appPath & "EmailTemplates\QuoteRequest.txt")
        mailMessage.Body = reader.ReadToEnd()
        mailCopy.Body = requestList.ToString()
        reader.Close()
        mailMessage.Body = mailMessage.Body.Replace("<%Name%>", tb_Name.Text)
        Dim client As SmtpClient = New SmtpClient()
        client.EnableSsl = True
        Try
            client.Send(mailMessage)
            client.Send(mailCopy)
            lbl_Confirmation.Text = "Thank you for your interest in our products. We have recieved your request. " _
                & "One of our team members will review your request and contact you shortly."
            lbl_Confirmation.Visible = True
            lbl_Confirmation.ForeColor = Drawing.Color.Green
            lbl_Confirmation.DataBind()
        Catch ex As Exception
            lbl_Confirmation.Text = ex.ToString()
            lbl_Confirmation.Visible = True
            lbl_Confirmation.ForeColor = Drawing.Color.Red
            lbl_Confirmation.DataBind()
        End Try
        MultiView1.SetActiveView(view_Confirmation)
    End Sub

    Protected Sub btn_Next_Click(sender As Object, e As EventArgs)
        MultiView1.SetActiveView(view_ContactInformation)
    End Sub

    Protected Sub btn_Back2Products_Click(sender As Object, e As EventArgs)
        Multiview1.SetActiveView(view_Products)
    End Sub

    Protected Sub btn_Next2Message_Click(sender As Object, e As EventArgs)
        MultiView1.SetActiveView(view_Message)
    End Sub

    Protected Sub btn_Back2ContactInformation_Click(sender As Object, e As EventArgs)
        MultiView1.SetActiveView(view_ContactInformation)
    End Sub


End Class